# -*- coding: utf-8 -*-
##############################################################################
#
#    INIT TECH, Open Source Management Solution
#    Copyright (C) 2012-2013 Tiny SPRL (<http://init.vn>).
#
##############################################################################

from openerp.osv import osv, fields
from openerp.tools.translate import _
from openerp import netsvc
import time

from datetime import datetime, timedelta
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP, float_compare
from dateutil.relativedelta import relativedelta
from openerp.osv import fields, osv
from openerp import netsvc
from openerp.tools.translate import _
import pytz
from openerp import SUPERUSER_ID

class load_document_type(osv.osv_memory):
    _name = "load.document.type"    
    _columns = {
        'name' : fields.char('Name', 64),
        'lines':fields.one2many('load.document.type.line', 'document_type_id', 'Lines'),     
        'portal_id': fields.many2one('portal.byf', 'Portal'),     
        'document_type_id': fields.many2one('document.type', 'Type'),    
        'date' : fields.datetime("Date"),
    }
    
    _defaults = {
        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),        
    }     
    
    def _line_for(self, cr, uid, line):
        balance_line = {'name': line.name,
                        }
        return balance_line     
  
    
    def default_get(self, cr, uid, fields, context=None):
        if context is None: context = {}
        res = super(load_document_type, self).default_get(cr, uid, fields, context=context)
        portal_ids = context.get('active_ids', [])
        portal_id, = portal_ids
        portal = self.pool.get('portal.byf').browse(cr, uid, portal_id)
        if 'portal_id' in fields:
            portal_id = portal.id
            res.update(portal_id = portal_id)   
        if 'name' in fields:
            name = portal.name
            res.update(name = name)   
        if 'document_type_id' in fields:
            document_type_id = portal.document_type_id.id
            res.update(document_type_id = document_type_id)                    
        if 'lines' in fields:
            lines = [self._line_for(cr, uid, line) for line in portal.document_type_id.document_line ]
            res.update(lines=lines)         
        return res     
    
    def action_save(self, cr, uid, ids, context={}):
        document_order = self.pool.get('document.order')
        for id in ids:
            wizzard = self.browse(cr, uid, id)
            for line in wizzard.lines:
                document_order.create(cr, uid,{
                                               'name': line.name,
                                               'datas': line.datas,
                                               'datas_fname': line.datas_fname,
                                               'portal_byf_id': wizzard.portal_id.id,
                                               'date': wizzard.date,
                                               })
            
        return True    
        
load_document_type()

class load_document_type_line(osv.osv_memory):
    _name = "load.document.type.line"    
    _columns = {   
        'name' : fields.char('Name', 64, required=True),
        'document_type_id':fields.many2one('load.document.type', 'Parent'),    
        'date' : fields.datetime("Date"),
        'datas': fields.binary('File', nodrop=True),
        'datas_fname': fields.char('File Name',size=256),           
    }
        
load_document_type_line()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
